package com.fy56.platform.goodsTracking.web.restful.api.controller;


import com.fy56.platform.goodsTracking.api.GoodsTrackApi;
import com.fy56.platform.goodsTracking.service.InfoQueryService;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultModel;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultStatus;
import com.wordnik.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.QueryParam;

/**
 * Created by XTK on 2016-07-25.
 */
@RestController
@RequestMapping("/api/v1/GoodsTracked")
public class QueryController {
    private static final Logger log = LoggerFactory.getLogger(QueryController.class);

    @Autowired
    private InfoQueryService infoQueryService;
    @Autowired
    private GoodsTrackApi goodsTrackApi;

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "货物跟踪查询")
    public ResponseEntity<ResultModel> query(@RequestParam(value = "tenant") String tenant,
                                             @RequestParam(value = "masterid") String masterid) {
        System.out.println("货物跟踪查询ConsignmentBillMasterId;tenant:"+tenant+";masterid:"+masterid);
        try {
            return new ResponseEntity(ResultModel.ok(infoQueryService.query(tenant,masterid)), HttpStatus.OK);
        } catch (IllegalArgumentException illegalArgumentException) {
            log.error("查询发送结果：参数错误:参数不符合验证规则,error:" + illegalArgumentException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, illegalArgumentException.getMessage()), HttpStatus.OK);
        } catch (NullPointerException nullPointerException) {
            log.error("查询发送结果: 参数错误:存在空值,error:" + nullPointerException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, nullPointerException.getMessage()), HttpStatus.OK);
        } catch (Exception exception) {
            log.error("查询发送结果:系统错误:error:" + exception.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, exception.getMessage()), HttpStatus.OK);
        }
    }



    @RequestMapping(value = "/seting", method = RequestMethod.POST)
    @ApiOperation(value = "默认设置")
    public ResponseEntity<ResultModel> seting() {
        System.out.println("货物跟踪--默认设置");
        try {
            return new ResponseEntity(ResultModel.ok(goodsTrackApi.list()), HttpStatus.OK);
        } catch (Exception exception) {
            log.error("货物跟踪--默认设置:系统错误:error:" + exception.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, exception.getMessage()), HttpStatus.OK);
        }
    }


}
